package com.umeox.watch.moto.poweroff;


import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.LayoutDirection;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.text.TextUtilsCompat;

import com.umeox.moto.watch.themes.skin.loader.ResourceManager;
import com.umeox.moto.watch.themes.util.UToast;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;

import java.util.Locale;

public class MainActivity extends ThemeAppCompatActivity implements View.OnClickListener {

    public static final String ACTION_REQUEST_SHUTDOWN = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
    public static final String EXTRA_KEY_CONFIRM = "android.intent.extra.KEY_CONFIRM";
    private TextView mPowerOffAb;
    private TextView mRebootAb;

    @Override
    protected void onStart() {
        super.onStart();
        if (DataProvider.isShutdownProhibited()) {
            showToast();
        }
    }

    private void showToast() {
        UToast.showCustomToast(this, getString(R.string.prower_off_disabled), Toast.LENGTH_LONG);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        mPowerOffAb = findViewById(R.id.poweroff_ab);
        mRebootAb = findViewById(R.id.restart_ab);
        mPowerOffAb.setOnClickListener(this);
        mRebootAb.setOnClickListener(this);

        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            mPowerOffAb.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ResourceManager.getInstance().getDrawable(R.drawable.power_off_icon), null);
            mRebootAb.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ResourceManager.getInstance().getDrawable(R.drawable.restart_icon), null);
        } else {
            mPowerOffAb.setCompoundDrawablesRelativeWithIntrinsicBounds(ResourceManager.getInstance().getDrawable(R.drawable.power_off_icon), null, null, null);
            mRebootAb.setCompoundDrawablesRelativeWithIntrinsicBounds(ResourceManager.getInstance().getDrawable(R.drawable.restart_icon), null, null, null);
        }
    }

    private long lastClickTimeMillis = 0L;

    @Override
    public void onClick(View v) {
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        switch (v.getId()) {
            case R.id.poweroff_ab:
                if (System.currentTimeMillis() - lastClickTimeMillis > 3000) {
                    lastClickTimeMillis = System.currentTimeMillis();
                    if (DataProvider.isActivated() && DataProvider.isReportShutdownLocation()) {
                        Intent intent1 = new Intent();
                        intent1.setAction(Constants.ACTION_POWEROFF);
                        sendBroadcast(intent1);

                        Intent intent = new Intent(MainActivity.this, UploadLocationActivity.class);
                        startActivity(intent);

                    } else {
                        Utils.shutdown(MainActivity.this);
                    }
                }

                break;
            case R.id.restart_ab:
                PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
                pm.reboot(null);
                break;
        }
    }

}
